﻿namespace CloudNinja.Metering.Authentication
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;

    public class WebRequestAdapter : IRequestAdapter
    {
        private readonly WebRequest request;

        public WebRequestAdapter(WebRequest request)
        {
            this.request = request;
        }

        public string Method
        {
            get
            {
                return this.request.Method;
            }
        }

        public string ContentType
        {
            get
            {
                return this.request.ContentType;
            }
        }

        public string RequestLocalPath
        {
            get
            {
                return this.request.RequestUri.LocalPath;
            }
        }

        public string Query
        {
            get
            {
                return this.request.RequestUri.Query;
            }
        }

        public string AuthorizationHeaderValue
        {
            get
            {
                return this.GetHeaderValue(HeaderNamesForHmac.AuthorizationHeaderName);
            }
        }

        public IEnumerable<string> HeadersKeys
        {
            get
            {
                return this.request.Headers.Keys.Cast<string>();
            }
        }

        public string DateHeader
        {
            get
            {
                return this.GetHeaderValue(HeaderNamesForHmac.DateHeader);
            }

            set
            {
                this.SetHeaderValue(HeaderNamesForHmac.DateHeader, value);
            }
        }

        public string UserHeader
        {
            get
            {
                return this.GetHeaderValue(HeaderNamesForHmac.UsernameHeaderName);
            }

            set
            {
                this.SetHeaderValue(HeaderNamesForHmac.UsernameHeaderName, value);
            }
        }

        public string GetHeaderValue(string headerName)
        {
            var webHeaderCollection = this.request.Headers;
            if (webHeaderCollection == null)
            {
                return string.Empty;
            }

            var headerValues = webHeaderCollection.GetValues(headerName);
            return headerValues != null ? string.Join(string.Empty, headerValues) : string.Empty;
        }

        private void SetHeaderValue(string headerName, string headerValue)
        {
            var webHeaderCollection = this.request.Headers;
            if (webHeaderCollection == null)
            {
                return;
            }

            var headerValues = webHeaderCollection.GetValues(headerName);
            if (headerValues == null)
            {
                this.request.Headers.Add(headerName, headerValue);
            }
            else
            {
                this.request.Headers.Set(headerName, headerValue);
            }
        }
    }
}